<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>加入班级</title>
    <!-- 引入 Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            background: url('../../static/images/styles.png') no-repeat center center fixed;
            background-size: cover;
            color: #333;
        }

        .container {
            background: rgba(255, 255, 255, 0.9);
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            max-width: 500px;
            margin: auto;
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #4CAF50;
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h2>加入班级</h2>
        <form id="joinClassForm">
            <div class="form-group">
                <label for="classId">请输入班级ID：</label>
                <input type="text" class="form-control" id="classId" name="classId" required>
            </div>
            <button type="submit" class="btn btn-primary btn-block">提交</button>
        </form>
    </div>
    <a href="/dashboard" class="btn btn-secondary">返回</a>
    <!-- 使用 Fetch API 提交表单 -->
    <script>
        document.getElementById('joinClassForm').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为

            // 获取用户输入的数据
            const classId = document.getElementById('classId').value;
            // 使用 Fetch API 发送数据到后端
            fetch('/classes/class/join_class', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    classId: classId
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    alert(data.error);
                }
                if (data.message){
                    alert(data.message);
                    // 跳转到班级列表页面
                    window.location.href = '/classes/classes';
                }
            })
            .catch(error => {
                // 捕获并显示错误信息
                alert(error.message);
            });

        });
    </script>
</body>
</html>
